package com.abigdreamer.dicux;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.abigdreamer.newspider.Article;
import com.abigdreamer.newspider.LoginFormInfo;
import com.abigdreamer.newspider.ThreadTaskManager;
import com.abigdreamer.util.FileUtil;

/**
 * 
 * @author Darkness
 * @date 2013-6-1 下午10:19:48
 * @website http://www.abigdreamer.com
 * @version V1.0
 */
public class ArticleUrlExtractor extends ThreadTaskManager {

	private static ArticleUrlExtractor instance = new ArticleUrlExtractor();

	public static ArticleUrlExtractor getInstance() {
		return instance;
	}

	private LoginFormInfo loginFormInfo;
	private ArticleUrlExtractor() {
	}

	public void extracte(LoginFormInfo loginFormInfo, String url, String host, int threadCount) {
this.loginFormInfo = loginFormInfo;
		// 进入翻页的处理
		for (int j = 1; j < 1001; j++) {// 从第一页翻到最后一页
			addTask("http://hkbici.com/forum-2-" + j + ".html");// 把URL存进去
		}

		for (int i = 0; i < threadCount; i++) {
			ArticleUrlExtractorProcesser fourmSpider = new ArticleUrlExtractorProcesser(this, loginFormInfo, host);
			addProcesser(fourmSpider);
		}

		startThread();
	}

	// 版块文章列表
	private Map<String, Map<String, String>> fourmArticles = new HashMap<String, Map<String, String>>();

	public Map<String, Map<String, String>> getfourmArticles() {
		return fourmArticles;
	}

	/**
	 * 添加文章列表到对应的版块下
	 * 
	 * @author Darkness
	 * @date 2013-6-1 下午08:42:54
	 * @website http://www.abigdreamer.com
	 * @version V1.0
	 */
	public void addArticleUrls(Map<String, String> articleurls, String url) {
		String fourmUrl = convertToFourmUrl(url);

		Map<String, String> articles = fourmArticles.get(fourmUrl);
		if (articles == null) {
			synchronized (this) {
				if (articles == null) {
					articles = new LinkedHashMap<String, String>();
					fourmArticles.put(fourmUrl, articles);
				}
			}
		}

		articles.putAll(articleurls);

		System.out.println("[" + fourmUrl + "]版块下共抓取到" + articles.size() + "个文章");
	}

	/**
	 * 找到版块根路径
	 * 
	 * @author Darkness
	 * @date 2013-6-1 下午08:48:56
	 * @website http://www.abigdreamer.com
	 * @version V1.0
	 */
	private static String convertToFourmUrl(String url) {
		Pattern pattern = Pattern.compile("forum-(\\d+)-(\\d+).html");
		Matcher matcher = pattern.matcher(url);
		if (matcher.find()) {
			int number = Integer.parseInt(matcher.group(2));
			if (number == 1) {
				return url;
			}
			return url.split("forum-")[0] + "forum-" + matcher.group(1) + "-1.html";
		}
		return url;
	}

	private boolean isDealAtricles = false;

	@Override
	protected void afterFinished() {
		if (isDealAtricles) {
			return;
		}

		synchronized (this) {
			if (isDealAtricles) {
				return;
			}

			isDealAtricles = true;

			ArticleSpider articleSpider = new ArticleSpider(this.loginFormInfo);
			articleSpider.process(getfourmArticles());
		}
	}

}
